﻿using Smartstore.Core.Content.Menus;
using Smartstore.Web.Rendering.Menus;
using Smartstore.Web.Models.Common;

namespace Smartstore.Web.Components
{
    public class FooterNavViewComponent : SmartViewComponent
    {
        private readonly IMenuService _menuService;

        public FooterNavViewComponent(
            IMenuService menuService)
        {
            _menuService = menuService;
        }

        public async Task<IViewComponentResult> InvokeAsync()
        {
            var customer = Services.WorkContext.CurrentCustomer;
            var isAdmin = customer.IsAdmin();
            var isRegistered = isAdmin || customer.IsRegistered();

            var model = new FooterNavModel
            {
                IsAuthenticated = isRegistered,
            };
            var menu = await _menuService.GetMenuAsync("Main");
            if (menu != null)
            {
                var menuModel = await menu.CreateModelAsync("Main", ViewContext);
                if (menuModel.Root != null && menuModel.Root.Children.Count > 0)
                {
                    var nodeFirst = menuModel.Root.Children[0].Value;
                    model.CategoryUrl = nodeFirst.GenerateUrl(ViewContext);
                }
            }
            return View(model);
        }
    }
}
